﻿using KYJControlLib;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using thinger.ControlLib;
using Timer = System.Windows.Forms.Timer;

namespace KYJScadaProModbusRtu
{
    public partial class FrmPumpControl : Form
    {
        public FrmPumpControl(int pumpIndex)
        {
            InitializeComponent();
           
            this.lbl_Title.Text = pumpIndex.ToString() + "号冷却泵控制";

            //动态绑定
            this.txts_Current.BindVarName = $"LQB{pumpIndex}_Current";
            this.txts_Speed.BindVarName = $"LQB{pumpIndex}_Speed";
            this.txts_TotalPower.BindVarName = $"LQB{pumpIndex}_TotalPower";
            this.txts_HeaterTemp.BindVarName = $"LQB{pumpIndex}_HeaterTemp";
            this.txts_Fre.BindVarName = $"LQB{pumpIndex}_Fre";
            this.txts_Power.BindVarName = $"LQB{pumpIndex}_Power";
            this.txts_RunHour.BindVarName = $"LQB{pumpIndex}_RunHour";

            this.btn_Mode.Tag = $"LQB{pumpIndex}_Mode";
            this.btn_Start.Tag = $"LQB{pumpIndex}_Start";
            this.btn_Stop.Tag = $"LQB{pumpIndex}_Stop";

            this.runState.BindVarName = $"LQB{pumpIndex}_RunState";
            this.FaultState.BindVarName = $"LQB{pumpIndex}_FaultState";

            //开启定时器
            this.updateTimer = new Timer();
            this.updateTimer.Interval = 200;
            this.updateTimer.Tick += UpdateTimer_Tick;
            this.updateTimer.Start();

            //FormClosing事件关闭定时器
            this.FormClosing += (sender, e) =>
            {
                this.updateTimer.Stop();
            };
        }

        private void UpdateTimer_Tick(object sender, EventArgs e)
        {
            //通用更新
            UpdateCommon(this);

            //单独更新
            bool value = false;
            if (bool.TryParse(CommomMethods.plcDevice[btn_Mode.Tag?.ToString()]?.ToString(), out value))
            {
                this.btn_Mode.Text = value ? "自动模式" : "手动模式";
                this.btn_Start.Enabled = this.btn_Stop.Enabled = !value;
            }
        }

        //实时更新的定时器
        private Timer updateTimer;

        #region 通用更新方法
        private void UpdateCommon(Control parentControl)
        {
            if (CommomMethods.plcDevice != null && CommomMethods.connectState)//设备连接状态
            {
                foreach (Control item in parentControl.Controls)
                {
                    //更新TextShow显示
                    if (item is TextShow textShow)
                    {
                        if (textShow.BindVarName != null && textShow.BindVarName.Length > 0)
                        {
                            float value = 0.0f;
                            if (float.TryParse(CommomMethods.plcDevice[textShow.BindVarName]?.ToString(), out value))
                            {
                                textShow.CurrentValue = value.ToString("f1");
                            }
                        }
                    }
                    //更新指示灯状态
                    else if (item is SimpleLed led)
                    {
                       if(led.BindVarName != null && led.BindVarName.Length > 0)
                        {
                            bool value = false;
                            if (bool.TryParse(CommomMethods.plcDevice[led.BindVarName]?.ToString(), out value))
                            {
                                led.State = value;
                            }
                        }
                    }
                    else if (item.HasChildren)//递归
                    {
                        UpdateCommon(item);
                    }
                }
            }
        }
        #endregion

        #region 关闭窗体事件
        private void btn_Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region 窗体拖动
        public static Point CPoint;

        private void Form_MouseDown(object sender, MouseEventArgs e)
        {
            CPoint = new Point(-e.X, -e.Y);
        }

        private void Form_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Point myPosittion = Control.MousePosition;//获取当前鼠标的屏幕坐标
                myPosittion.Offset(CPoint.X, CPoint.Y);//重载当前鼠标的位置
                this.DesktopLocation = myPosittion;//设置当前窗体在屏幕上的位置
            }
        }
        #endregion

        private void btn_Mode_Click(object sender, EventArgs e)
        {
            if (sender is Button button)
            {
                if (button.Tag != null && button.Tag.ToString().Length > 0)
                {
                    //取反写入
                    CommomMethods.CommonWrite(button.Tag.ToString(), (button.Text != "自动模式").ToString());
                }
            }
        }

        private void btn_OP_Click(object sender, EventArgs e)
        {
            if (sender is Button button)
            {
                if (button.Tag != null && button.Tag.ToString().Length > 0)
                {
                    //写入沿信号，先置位再复位
                    CommomMethods.CommonWrite(button.Tag.ToString(), "True");
                    Thread.Sleep(100);
                    CommomMethods.CommonWrite(button.Tag.ToString(), "False");
                }
            }
        }

        #region 减少闪烁
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams parss = base.CreateParams;
                parss.ExStyle |= 0x02000000;
                return parss;
            }
        }
        #endregion
    }
}
